import facecat from "facecat"
import facecat_ws from "facecat_ws"

let gPaint = new facecat.FCPaint();
let that = null;

// index.js
// 获取应用实例
const app = getApp()
Page({
    data: {
        canvasWidth:0,
        canvasHeight:0,
        facecatStyle:"",
        facecatContent:"",
        facecatFocus:false
      },
      bindInputChange: function(e) {  
        if(gPaint.editingTextBox){
            gPaint.editingTextBox.text = e.detail.value;
            facecat.invalidateView(gPaint.editingTextBox);
            gPaint.editingTextBox = null;
            gPaint.app.setData({
                facecatStyle: "position:absolute;left:0px;top:0px;width:0px;height:0px;display:none;", 
                facecatFocus:false,
                facecatContent:""
             })
        }
      },
      touchStartCanvas: function (e) {
        facecat_ws.handleTouchStart(gPaint, e, null);
      },touchMoveCanvas: function (e) {
        facecat_ws.handleTouchMove(gPaint, e, null);
      },touchEndCanvas: function (e) {
        facecat_ws.handleTouchEnd(gPaint, e, null, null);
      },
  onLoad() {
      that = this;
    wx.createSelectorQuery().select('#myCanvas').fields({ node: true, size: true }).exec((cres) => {       
        wx.getSystemInfo({
            success: function(res) {
                that.setData({
                    canvasWidth: res.windowWidth,  
                    canvasHeight: res.windowHeight
                 })
                cres[0].width = res.windowWidth;
                cres[0].height = res.windowHeight;
            }
        });   
        facecat_ws.scaleCanvas(gPaint, cres);
        //解析加载XML
        let xml = `<?xml version="1.0" encoding="utf-8"?>
        <html xmlns="facecat">
          <head>
          </head>
          <body>
            <div dock="fill" type="tab" selectedindex="0">
              <div name="TabPage" text="可视化生成" type="tabpage">
                <div type="layout" name="LayoutDiv" text="LayoutDiv" location="30,30" size="265,99" showhscrollbar="True">
                  <input type="button" name="Button" text="Button" location="27,26" size="100,20" />
                  <input type="button" name="Button2" text="Button2" location="27,26" size="100,20" />
                  <input type="button" name="Button3" text="Button3" location="27,26" size="100,20" />
                  <input type="button" name="Button4" text="Button4" location="27,26" size="100,20" />
                  <input type="button" name="Button5" text="Button5" location="27,26" size="100,20" />
                  <input type="button" name="Button6" text="Button6" location="27,26" size="100,20" />
                </div>
                <div type="layout" name="LayoutDiv2" text="LayoutDiv2" location="71,210" size="135,234" layoutstyle="TopToBottom" showvscrollbar="True">
                  <input type="button" name="Button7" text="Button7" location="27,26" size="100,100" />
                  <input type="button" name="Button8" text="Button8" location="27,26" size="100,100" />
                  <input type="button" name="Button9" text="Button9" location="27,26" size="100,100" />
                  <input type="button" name="Button10" text="Button10" location="27,26" size="100,100" />
                  <input type="button" name="Button11" text="Button11" location="27,26" size="100,100" />
                  <input type="button" name="Button12" text="Button12" location="27,26" size="100,100" />
                </div>
              </div>
              <div name="TabPage2" text="代码创建" type="tabpage" />
            </div>
          </body>
        </html>
        `;
	    facecat_ws.renderFaceCatWeChat(gPaint, xml, that);
        //获取第二页
        let tabPage = facecat.findViewByName("TabPage2", gPaint.views);
        let layoutDiv = new facecat.FCLayoutDiv();
        layoutDiv.location = new facecat.FCPoint(50, 50);
        layoutDiv.size = new facecat.FCSize(250, 100);
        layoutDiv.showHScrollBar = true;
        facecat.addViewToParent(layoutDiv, tabPage);
        for (let i = 0; i < 10; i++) {
            let button = new facecat.FCButton();
            button.location = new facecat.FCPoint(0, 0);
            button.size = new facecat.FCSize(100, 40);
            button.backColor = "rgb(255,255,255)";
            button.textColor = "rgb(0,0,0)";
            button.borderColor = "rgb(150,150,150)";
            facecat.addViewToParent(button, layoutDiv);
            button.text = "Button";
        }
        facecat.resetLayoutDiv(layoutDiv);
    })
  }
})
